<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
    exit();
}

?>
<meta charset="UTF-8">
<?php

if ($_GET['page'] == '') {
    $start = 1;
    $page = 1;
} else {
    $start = ($_GET['page'] - 1) * 20 + 1;
    $page = $_GET['page'];
}

$usersResultSet = getUsers($start, $start + 29);

$users = countUsers();
$pages = pagesFromUsers($users);

$pagesStart = 0;
$pagesEnd = 0;

$page = $pages;

if ($pages <= 5) {
    $pagesStart = 1;
    $pagesEnd = $pages;
} else if ($pages >= 5 && $page + 2 <= $pages) {
    $pagesStart = $page - 2;
    $pagesEnd = $page + 2;
} else if ($pages >= 5 && $page + 2 > $pages) {
    $pagesStart = $pages - 4;
}

?>

<div class="userscontent">
    <div class="blockheader">
        Все пользователи
    </div>

    <div id="pages">
        <?php
        if ($pages == 0 or $pages == 1) {

        } else {
            if ($page > 1) {
                ?>
                <div class="page"
                     onclick="location.href='admin/index.php?block=allusers&page=1'">
                    <<
                </div>
            <?php
            }

            for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                ?>
                <div class="page"
                     onclick="location.href='admin/index.php?block=allusers&page=<?= $i ?>'">
                    <?= $i ?>
                </div>
            <?php
            }
            if ($page != $pages) {
                ?>
                <div class="page"
                     onclick="location.href='admin/index.php?block=allusers&page=<?= $pages ?>'">
                    >>
                </div>
            <?php
            }
        }
        ?>
    </div>

    <?php
    while ($user = mysql_fetch_assoc($usersResultSet)) {
        ?>
        <div class="user" onclick="location.href='../profile.php?uid=<?= $user['id'] ?>'">
            <div class="usertd">
                <?= $user['nick'] ?>
            </div>
            <div class="usertd">
                <?= $user['registered'] ?>
            </div>
            <div class="usertd">
                <?= $user['email'] ?>
            </div>

        </div>
        <div class="blockuserbutton" onclick="location.href='block_user.php?uid=<?= $user['id'] ?>'">
            Блокировать
        </div>
    <?
    }
    ?>

</div>

